layui.use(['layer','upload','laydate','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
    var layedit = layui.layedit;
    var laydate = layui.laydate;


    var remarks;
    //富文本编辑器
    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/upload/editPicUpload' //接口url
                ,type: 'post' //默认post
            }

        });
        remarks=layedit.build('remarks',{
            height: 160 //设置编辑器高度
        }); //建立编辑器

    })






    //获取路径参数
    var request = GetRequest();
    $("#produceBatchId").val(request.id);
    $("#processId").val(request.processId);

    //加载投入品列表
    $.get("/invest/list",function (data) {
        $.each(data.data,function (i,ele) {
            $("#investId").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
        });
        form.render('select');
        //同步投入品名称
        form.on('select(investId)',function (data) {
            $.get("/invest/" + data.value,function (data) {
                $("#name").val(data.name);
            });
        });
    });

    //时间组件
    laydate.render({
        elem: '#investTime'
    });

    //添加企业信息
    form.on('submit(add)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        data.field.remarks= layui.layedit.getContent(remarks);
        //初始化数据
        $.ajax({
            url: '/record/invest/add',
            data:data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

